<?php
// 接口-物料档案类
namespace interface\pda;

use control\inventoryBalanceClass;
use control\machiningOrderClass;

class materialClass extends index
{
	private const page     = 'material';     # 权限
	private const table    = 'material';     # 主表
	private const tableSku = 'material_sku'; # 子表
	
	public function  list()
	{
		$barcode = $this->post('barcode') ?: '';
		$waid = $this->post('waid') ?: '';
		// $from = $this->post('from') ?: '';
		$code = $this->post('wd') ?: '';
		$where = " AND m.coid = '" . static::$coid . "' ";
		if ($code) {
			$where .= " AND (m.code like '%$code%' or m.name like '%$code%') ";
		}
		if ($barcode) {
			$where .= " AND sku.barcode like '%$barcode%' ";
		}
		$page           = $this->page([
			"table" => static::table . ' as m,' . static::tableSku . ' as sku',
			"field" => ' m.*, sku.id as skuId, sku.name as skuName ',
			"where" => ' m.id = sku.materialId ' . $where,
			"order" => " m.time desc",
		]);
		$data['result'] = [];
		foreach ($page['array'] as $array) {
			$data['result'][] = array(
				'id'           => $array['id'],
				'skuId'        => $array['skuId'],
				'code'         => $array['code'],
				'name'         => $array['name'],
				'brand'        => $array['brand'],
				'model'        => $array['model'],
				'skuName'      => $array['skuName'],
				'unit'         => $array['unit'],
				'batch'        => static::getId(),
				'inventoryNum' => inventoryBalanceClass::inventoryNum($array['skuId'], $waid),
				// 可用库存计算
				'usableNum'    => machiningOrderClass::inventory($array['skuId'], $array['unit'], $waid),
				'unitArray'    => inventoryBalanceClass::materialUnit($array['id']),
			);
		}
		$data['page']       = $page['para'];
		$this->resp['data'] = $data;
		$this->code         = 1;
	}
}
